package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.ulits.DateUtils;
import com.itheima.ulits.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 体检预约业务实现
 * @author ：zengjie
 * @date ：2020/11/14
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;
    /**
     * 体检预约
     *{"setmealId":"12","sex":"1","telephone":"15111112222",
     * "idCard":"430426199904128952","validateCode":"1231"}
     *
     * @param map
     * @return
     */
    @Override
    public Result submit(Map map) {
        try {
            String telephone = (String) map.get("telephone");//预约电话
            String orderDate = (String) map.get("orderDate");//预约时间
            String name = (String) map.get("name");//名称
            String sex = (String) map.get("sex");//性别
            String idCard = (String) map.get("idCard");//身份证
            String orderType = (String) map.get("orderType");//预约类型
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));//套餐id
            Date date = DateUtils.parseString2Date(orderDate);//预约时间转换为Date格式
            OrderSetting os = orderSettingDao.findByOrderDate(orderDate);
            //判断是否为空,为空就返回错误信息
            if (os == null) {
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }
            //再查询当前日期是否约满
            int reservations = os.getReservations();//已经预约人数
            int number = os.getNumber();//最大可预约人数
            if (reservations >= number) {
                return new Result(false, MessageConstant.ORDER_FULL);
            }
            //查看当前手机号是否为会员
            Member member = memberDao.findByTelephone(telephone);
            if (member != null){
                //不为空,查看是否存在重复预约
                Order order = new Order();
                //补全数据
                //套餐id
                order.setSetmealId(setmealId);
                //预约日期
                order.setOrderDate(date);
                //会员id
                order.setMemberId(member.getId());
                //查询是否存在已经预约的相同套餐
                List<Order> listOrder = orderDao.findByCondition(order);
                if (listOrder != null && listOrder.size() > 0) {
                    //存在就返回错误信息
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
            }else{
                //为空则自动注册
                member = new Member();
                //姓名
                member.setName(name);
                //性别
                member.setSex(sex);
                //身份证
                member.setIdCard(idCard);
                //手机号码
                member.setPhoneNumber(telephone);
                //注册时间
                member.setRegTime(new Date());
                //添加用户
                memberDao.add(member);
            }
            //进行新增预约信息,
            Order order = new Order();
            //补全数据
            order.setMemberId(member.getId());//会员id
            order.setOrderDate(date);//预约日期
            order.setOrderType(orderType);//预约类型 电话预约 微信预约
            order.setOrderStatus(Order.ORDERSTATUS_NO);//默认未到诊
            order.setSetmealId(setmealId);//套餐id

            orderDao.add(order);

            //将OrderSetting表中的预约人数+1
            os.setReservations(os.getReservations() + 1);
            orderSettingDao.editReservationsByOrderDate(os);

            //发送成功预约短信
            if (false)
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, orderDate);
            System.out.println("手机号码："+telephone+"预约日期："+orderDate);
            //返回成功信息
            return new Result(true,MessageConstant.ORDER_SUCCESS,order);
        } catch (Exception e) {
            e.printStackTrace();
            //返回系统错误异常
            return new Result(false,MessageConstant.SYSTEM_FAIL);
        }
    }

    /**
     * 根据预约id查询预约信息
     * @param id
     * @return
     */
    @Override
    public Map findById4Detail(Integer id) {
        return orderDao.findById4Detail(id);
    }
}
